unit smGUIButton;

interface

uses
  smGUI, smGUIPanel, smGUIRender, smCommonTypes, Data.DBXJSON;

type
  TGUIButton = class(TGUIControl)
  protected
    var FDown: Boolean;
    var PositionChanged: Boolean; // by mouse left click
    var FCaption: AnsiString;
    var FCommand: AnsiString;

    procedure OnMouseDown(const Button: Byte); override;
    procedure OnMouseUp(const Button: Byte); override;
    procedure ParseJSON(const Source: TJSONPair); override;

  public
    property MouseDown: Boolean read FDown;
    property Caption: AnsiString read FCaption write FCaption;
    property Command: AnsiString read FCommand write FCommand;

    procedure Perform(const Message: TGUIMessage; const Lo, Hi: Integer;
      var Handled: Boolean); override;
    procedure Render(const ARenderer: Pointer); override;
  end;

  TGUIHideButton = class(TGUIButton)
  protected
    procedure OnMouseUp(const Button: Byte); override;
  public
    constructor Create(const APanel: TGUIPanel); override;
  end;

implementation

uses
  smLuaUtils, smConst;

{ TGUIButton }

procedure TGUIButton.OnMouseDown(const Button: Byte);
begin
  if not MouseDown and (State = gesHovered) and (Button = 0) then
  begin
    FDown := True;
    if not Assigned(Background) then
    begin
      Position.SetValue(
        Position.Left + 1, Position.Top + 1,
        Position.Width, Position.Height
      );
      PositionChanged := True;
    end;

  end;
end;

procedure TGUIButton.OnMouseUp(const Button: Byte);
begin
  if MouseDown and (Button = 0) then
    AppendCommand(Command);
end;

procedure TGUIButton.ParseJSON(const Source: TJSONPair);
begin
  inherited;

  // Additional properties
  with Source, Source.JsonString do
    if Value = 'command' then
      Command := AnsiString(JsonValue.Value)

    else if Value = 'caption' then
      Caption := AnsiString(JsonValue.Value);
end;

procedure TGUIButton.Perform(const Message: TGUIMessage; const Lo, Hi: Integer;
  var Handled: Boolean);
begin
  inherited;
  // no button dragging option
  FDrag := False;

  // Mouse up - release "pressed" state
  if MouseDown and (Message = gmMouseUp) and (Lo = 0) then
  begin
    FDown := False;
    if PositionChanged then
    begin
      Position.SetValue(
        Position.Left - 1, Position.Top - 1,
        Position.Width, Position.Height
      );
      PositionChanged := False;
    end;
  end;
end;

procedure TGUIButton.Render;
begin
  with TGUIRender(ARenderer) do
    DrawButton(Self);

end;

{ TGUIHideButton }

constructor TGUIHideButton.Create(const APanel: TGUIPanel);
begin
  inherited;
  FPosition.Width := 15;
  FPosition.Height := 15;
  FPosition.Left := Owner.Width - 15 - Owner.ClientRect.Left;
  FPosition.Top  := -Owner.ClientRect.Top;
  Caption := 'X';
end;

procedure TGUIHideButton.OnMouseUp(const Button: Byte);
begin
  if MouseDown and (Button = 0) then
  begin
    if Owner.Owner.ModalPanel = Owner then
      Owner.Owner.ReleaseModal(ModalResult_CANCEL)
    else
      Owner.Visible := False;

    AppendCommand(Command);
  end;
end;

end.
